var myBusyIndicator = new WL.BusyIndicator('main',{text : "waiting..."});

/**
 * initialize the book list
 */
function initializePage() {
//	var invocationData = {
//			adapter : 'stAdp',
//			procedure : 'getHistoryBook',
//			parameters : [{'dri_id': window.driverInfo.id}]
//	};
//	myBusyIndicator.show();
//	WL.Client.invokeProcedure(invocationData, {
//		onSuccess : getHistoryBookSuccess,
//		onFailure : getHistoryBookFailure
//	});
	findBookSuccess({});
}

/**
 * get find book success, initialize the page according to the result
 */
function findBookSuccess(result) {
//	myBusyIndicator.hide();
	var testData = [ {
		'schedule_id' : '1',
		'date' : '08/22/2013',
		'orderList' : [ {
			'order_id' : '1',
			'setupTime' : '09:00',
			'fromAddr' : 'shanghai zhangjiang',
			'toAddr' : 'shanghai Xuhui',
			'cus_id' : '1',
			'nickname' : 'Bai YuShi',
			'credit' : 5
		}, {
			'order_id' : '2',
			'setupTime' : '10:30',
			'fromAddr' : 'shanghai Xuhui',
			'toAddr' : 'shanghai huochezhan',
			'cus_id' : '12',
			'nickname' : 'Bai YuShi',
			'credit' : 4
		} ]
	}, {
		'schedule_id' : '2',
		'date' : '08/22/2013',
		'orderList' : [ {
			'order_id' : '3',
			'setupTime' : '09:00',
			'fromAddr' : 'shanghai zhangjiang',
			'toAddr' : 'shanghai Xuhui',
			'cus_id' : '1',
			'nickname' : 'Zheng MingFeng',
			'credit' : 5
		}, {
			'order_id' : '4',
			'setupTime' : '10:30',
			'fromAddr' : 'shanghai Xuhui',
			'toAddr' : 'shanghai huochezhan',
			'cus_id' : '12',
			'nickname' : 'Samul Lu',
			'credit' : 4
		} ]
	} ];
	$.each(testData, function(i, item) {
		var itemContent = "<div class='scheduleItem'><div class='scheduleItemTitle'>";
		itemContent = itemContent.concat("<div class='leftCommon boldFont bigFont'>" + item.date + "</div>");
		itemContent = itemContent.concat("<div class='rightCommon boldFont bigFont'>" + item.orderList.length + " orders</div>");
		itemContent = itemContent.concat("<p style='clear: both'></p>");
		itemContent = itemContent.concat("<div class='leftCommon'>" + item.orderList[0].fromAddr + "</div>");
		itemContent = itemContent.concat("<div class='rightCommon'>" + item.orderList[0].setupTime + "</div>");
		itemContent = itemContent.concat("<div class='leftCommon'>" + item.orderList[item.orderList.length - 1].toAddr + "</div>");
		itemContent = itemContent.concat("<div class='rightCommon acceptSchedule' onclick='acceptSchedule(" + item.schedule_id + ")'><a><img src='../images/acceptBtn.png' width='30px' height='30px'></a></div>");
		itemContent = itemContent.concat("<p style='clear: both'></p></div>");
		itemContent = itemContent.concat("<div class='scheduleDetail'>");
		$.each(item.orderList, function(k, order) {
			itemContent = itemContent.concat("<div class='orderWrapper'><img class='orderDot' src='../images/dot.png' width='14px' height='14px'/>");
			itemContent = itemContent.concat("<div class='leftCommon boldFont scheduleOrderFrom'>" + order.fromAddr + "  <img src='../images/arrow_Icon.png' width='28px' height='12px' />" + order.toAddr + "</div>");
			itemContent = itemContent.concat("<p style='clear: both'></p>");
			itemContent = itemContent.concat("<div class='leftCommon'>" + order.setupTime + "</div><p style='clear: both'></p>");
			itemContent = itemContent.concat("<div class='leftCommon smallFont grayFont'>" + order.nickname + "</div><div class='leftCommon'>");
			for(var j = 0; j < order.credit; j++) {
				itemContent = itemContent.concat("<img src='../images/star.png' width='14px' height='14px'>");
			}
			itemContent = itemContent.concat("</div><p style='clear: both;'></p></div>");
		});
		itemContent = itemContent.concat("<div class='rightCommon returnBg'><a><img src='../images/return.png' width='30px' height='30px'/></a></div>");
		itemContent = itemContent.concat("<p style='clear: both;'></p></div></div>"); 
		$(itemContent).appendTo($("#findScheduleList"));
	});
	
}

/**
 * accept the schedule
 * @param schedule_id
 */
function acceptSchedule(schedule_id) {
	window.clickAccept = 1;
	alert(schedule_id);
}

/**
 * get history book fail, remind the user
 */
function getHistoryBookFailure() {
	alert("getHistoryBookFailure");
}
(function($){
	$(".backBtn").on('tap', function() {
		$.mobile.changePage("driver-main.html");
	});
	initializePage();
	$(".scheduleItemTitle").on('tap', function() {
		if(window.clickAccept == 1) {
			window.clickAccept = 0;
			return;
		}
		$(this).next().slideToggle("fast");
	});
	$(".returnBg").on('tap', function() {
		$(this).parent().slideToggle("fast");
	});
})(jQuery);